﻿using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Estsh.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Estsh.Core.Controllers;
using Newtonsoft.Json;

namespace Estsh.Core.Web.Plugin.Wms.Areas.wms.Controllers.Material
{
    [Route("wms/[controller]")]
    [ApiController]
    public class MaterialController : Controller
    {
        private IMaterialService service;

        // GET: LoginController
        public MaterialController(IMaterialService _service)
        {
            service = _service;
        }

        //拆分 判断箱条码
        [HttpPost("GetMaterialInfo")]
        public IActionResult GetMaterialInfo(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.GetMaterialInfo(cartonNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckMaterialSplit")]
        public IActionResult CheckMaterialSplit(Newtonsoft.Json.Linq.JObject jobj)
        {
            int splitNum = Convert.ToInt32(jobj["splitNum"].ToString());
            string loginId = jobj["loginId"].ToString().Trim();
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            int splitCount = Convert.ToInt32(jobj["splitCount"].ToString());
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckMaterialSplit(cartonNo, splitNum, loginId, splitCount + 1);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckMaterialConsolidation")]
        public IActionResult CheckMaterialConsolidation(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string secondaryCartonNo = jobj["secondaryCartonNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckMaterialConsolidation(cartonNo, secondaryCartonNo, loginId);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        
        [HttpPost("ScanCode")]
        public IActionResult ScanCode(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.ScanCode(cartonNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        [HttpPost("SpellCode")]
        public IActionResult SpellCode(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.SpellCode(cartonNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("TailBox")]
        public IActionResult TailBox(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.TailBox(cartonNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }
    }
}
